package com.example.opengldemo.mirror;

import android.content.Context;
import android.opengl.GLES30;
import android.view.MotionEvent;

import com.example.opengldemo.R;
import com.example.opengldemo._112_.BaseView;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static com.example.opengldemo.mirror.BallFromControl.FLOOR_Y;

public class MyMirrorView extends BaseView {
    Ball ball ;
    BallFromControl ballFromControl ;
    Mirror mirror ;
    public MyMirrorView(Context context) {
        super(context);
    }
    private float mPreviousY;
    private float mPreviousX;
    private final float TOUCH_SCALE_FACTOR = 180.0f/360;
    float cr=24;
    float cAngle=0;
//    @Override
//    public boolean onTouchEvent(MotionEvent e) {
//        float y = e.getY();
//        float x = e.getX();
//        switch (e.getAction()) {
//            case MotionEvent.ACTION_MOVE:
//                float dy = y - mPreviousY;
//                float dx = x - mPreviousX;
//                cAngle+=dx * TOUCH_SCALE_FACTOR;
//                MatrixState.cameraLocation[0]=(float) (Math.sin(Math.toRadians(cAngle))*cr);
//                MatrixState.cameraLocation[2]=(float) (Math.cos(Math.toRadians(cAngle))*cr);
//                MatrixState.cameraLocation[1]+=dy/10.0f;
//        }
//        mPreviousY = y;//º«¬º¥•øÿ± Œª÷√
//        mPreviousX = x;//º«¬º¥•øÿ± Œª÷√
//        return true;
//    }
    int[] textureId = new int[10];
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        super.onSurfaceCreated(gl10 , eglConfig);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        //GLES30.glEnable(GLES30.GL_CULL_FACE);
        ball = new Ball(this) ;
        ballFromControl = new BallFromControl(ball ,10f) ;
        mirror = new Mirror(this) ;
        MatrixState.setInitStack();
        textureId[0] = TextureUtil.getTextureId(this , R.drawable.mdb , null, false) ;
        textureId[1] = TextureUtil.getTextureId(this , R.drawable.basketball, null, false) ;

    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        super.onSurfaceChanged(gl10 , i , i1);
        float des = (float)i/i1 ;
        MatrixState.setProjectFrustum(-des , des  ,-1 , 1 , 1 , 1000 );
        MatrixState.setCamera(0.0f,1f,30f,0,0f,0,0,1,0);
    }
    @Override
    public void onDrawFrame(GL10 gl10) {
        super.onDrawFrame(gl10);
        MatrixState.setCamera(MatrixState.cameraLocation[0],MatrixState.cameraLocation[1],MatrixState.cameraLocation[2],0f,0f,0f,0f,1.0f,0.0f);
        MatrixState.pushMatrix();
        MatrixState.translate(0, -2,0);
        MatrixState.rotate(90, 1,0, 0);
        MatrixState.translate(0, FLOOR_Y,0);
        mirror.drawSelf(textureId[0]);
        MatrixState.popMatrix();

        MatrixState.pushMatrix();

        ballFromControl.drawSelfMirror(textureId[1]);//ªÊ÷∆æµœÒÃÂ
        ballFromControl.drawSelf(textureId[1]);//ªÊ÷∆ µº ŒÔÃÂ
        MatrixState.popMatrix();

    }

}
